package cn.javabb.project.service;

import cn.hutool.core.collection.CollUtil;
import cn.javabb.project.entity.SysDataSource;
import cn.javabb.project.entity.SysTemplate;
import cn.javabb.project.mapper.TemplateMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2021/03/26 18:12
 */
@Slf4j
@Service
public class TemplateService extends ServiceImpl<TemplateMapper, SysTemplate> {

    public List<SysTemplate> list(SysTemplate st) {
        return baseMapper.list(st);
    }

    /**
     * 根据groupId 获取模版列表
     * @param groupId
     * @return
     */
    public List<SysTemplate> listByGroupId(Long groupId) {
        QueryWrapper wrapper = new QueryWrapper<SysTemplate>().eq("group_id",groupId).orderByDesc("create_time");
        return baseMapper.selectList(wrapper);
    }

    public boolean deleteByGroupId(Long groupId) {
        List<SysTemplate> templates = this.listByGroupId(groupId);
        List<Long> ids = templates.stream().map(SysTemplate::getId).collect(Collectors.toList());
        return removeByIds(ids);
    }


    public static void main(String[] args) {
        List<String> names = CollUtil.toList("a", "b");
        List<String> formatNames = names.stream().map(t->"`"+t+"`").collect(Collectors.toList());
        formatNames.forEach(System.out::print);
    }
}
